package com.lagou.edu.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: ruanyaguang
 * @Date : 2020/4/24
 * @Info :
 */
@Component
public class SessionValidateInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean result = false;
        if (null != request.getSession().getAttribute("username")) {
            result = true;
        }
        if (!result) {
            response.sendRedirect(request.getContextPath() + "/login");
        }
        return result;
    }
}